package com.rabbit.helloword;

import java.io.IOException;

import com.rabbit.RabbitFactory;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;

/**
 * @author CUIJIAJUN
 * @date   2017年2月15日
 * 
 */
public class Receive {
	private final static String QUEUE_NAME = "helloword";

	public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
		Connection connection = RabbitFactory.creatConnection();
		Channel channel = connection.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		QueueingConsumer consumer = new QueueingConsumer(channel);
		
		channel.basicConsume(QUEUE_NAME,true,consumer);
		while(true){
			//nextDelivery是一个阻塞方法（内部实现其实是阻塞队列的take方法）    
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();    
            String message = new String(delivery.getBody());    
            System.out.println("Received Message：'" + message + "'");    
		}
		
	}
}
